package com.example.yrdev.ordering_system.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;

import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by luke on 12/25/16.
 */

public class DatePickerDialog extends Dialog {

    @InjectView(R.id.start_date)DatePicker startDate;


    public interface DateListener{

        void getDate(String startDate, Date date);

    }
    private DateListener dateListener;


    public DatePickerDialog(Context context){
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

    }

    public void setDateListener(DateListener dateListener) {
        this.dateListener = dateListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date_picker_layout);
        ButterKnife.inject(this);



    }
    public Date getCurrentDate() throws ParseException{
        Calendar c=Calendar.getInstance();
        c.set(startDate.getYear(),startDate.getMonth(),startDate.getDayOfMonth());

        return     c.getTime();




    }

    boolean yearHide=false;

    public void hideYear(){

        int year = getContext().getResources()
                .getIdentifier("android:id/year", null, null);

        if (year!=0){
            View yearPicker=startDate.findViewById(year);
            yearPicker.setVisibility(View.GONE);
        }
        yearHide=true;


    }



    @OnClick(R.id.confirm)
 void confirm(){

        String tempStartDate=startDate.getYear()+"."+(startDate.getMonth()+1)+"."+startDate.getDayOfMonth();
        if (yearHide)
            tempStartDate=(startDate.getMonth()+1)+"."+startDate.getDayOfMonth();

        try {
            if (dateListener!=null)
                dateListener.getDate(tempStartDate,getCurrentDate());


        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());

        }

       dismiss();

    }
    @OnClick(R.id.close)
    void closeDialog(){

        dismiss();

    }
}
